﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;

using Dundas.Charting.WebControl;
using Wind.MultimediaStation.WebService.Entity;
using Wind.Utils;

namespace Wind.MultimediaStation.WebService
{
    public class DrawCompareChart : BaseChart
    {

        public virtual Chart GetChart(StockQuote StockQuote1, StockQuote StockQuote2)
        {
            //chart 基本信息
            Chart myChart = new Chart();

            try
            {
                myChart.Width = 1920;
                myChart.Height = 1024;
                myChart.BackColor = Color.Black;
                myChart.Palette = ChartColorPalette.Vegas;
                myChart.Legends["Default"].Enabled = false;

                //过滤行情数据1
                FilterQuoteData(StockQuote1);
                //过滤行情数据2
                FilterQuoteData(StockQuote2);

                #region titles
                DrawTitles(StockQuote1, StockQuote2, myChart);
                #endregion

                #region ChartAreas
                DrawChartAreas(StockQuote1, StockQuote2, myChart);
                #endregion

                //处理行情曲线1和昨日收盘价曲线1
                DrawStockQuoteSeries1(StockQuote1, myChart);

                //处理行情曲线2和昨日收盘价曲线2
                DrawStockQuoteSeries2(StockQuote2, myChart);

            }
            catch (Exception e)
            {
                LogHelper.Write(e.Message, e);
            }
            return myChart;
        }

        /// <summary>
        /// 左侧分时图
        /// </summary>
        /// <param name="StockQuote1"></param>
        /// <param name="myChart"></param>
        private static void DrawStockQuoteSeries1(StockQuote StockQuote1, Chart myChart)
        {
            if (StockQuote1 != null)
            {
                //处理分时行情
                Series SeriesStockQuote = new Series("StockQuote1");
                myChart.Series.Add(SeriesStockQuote);
                SeriesStockQuote.BorderColor = Color.White;
                SeriesStockQuote.BorderWidth = 4;
                SeriesStockQuote.BackGradientEndColor = ColorTranslator.FromHtml("100,3,39,110");//050808   #0A0F17
                SeriesStockQuote.Color = ColorTranslator.FromHtml("255,3,39,110");//0000FF  #01286E
                SeriesStockQuote.Type = SeriesChartType.SplineArea;
                SeriesStockQuote.CustomAttributes = "DrawingStyle=LightToDark";
                SeriesStockQuote.BackGradientType = GradientType.TopBottom;

                if (StockQuote1 != null)
                {
                    SeriesStockQuote.Points.DataBindXY(StockQuote1.QuoteDateList, StockQuote1.QuotePriceList);
                }
                SeriesStockQuote.ChartArea = "ChartArea1";

                //处理休市时的时间点
                CustomerPauseLabel(StockQuote1, SeriesStockQuote);

                //处理昨日收盘价
                Series seriesYestodayPrice = new Series("YestodayPrice1");
                myChart.Series.Add(seriesYestodayPrice);

                List<string> QuoteDateListTemplate = CreateQuoteDateListTemplate(StockQuote1);
                if (QuoteDateListTemplate != null)
                {
                    for (int i = 0; i < QuoteDateListTemplate.Count; i++)
                    {
                        double yestodayPrice = StockQuote1.YestodayPrice;
                        seriesYestodayPrice.Points.AddXY(QuoteDateListTemplate[i], yestodayPrice);
                    }
                }

                seriesYestodayPrice.Type = SeriesChartType.Line;
                seriesYestodayPrice.Color = ColorTranslator.FromHtml("150, 255, 255, 255");
                seriesYestodayPrice.BorderWidth =3;
                seriesYestodayPrice.BorderStyle = ChartDashStyle.Solid;
                seriesYestodayPrice.ChartArea = "ChartArea1";
            }
        }

        /// <summary>
        /// 右侧分时图
        /// </summary>
        /// <param name="StockQuote2"></param>
        /// <param name="myChart"></param>
        private static void DrawStockQuoteSeries2(StockQuote StockQuote2, Chart myChart)
        {
            if (StockQuote2 != null)
            {
                //处理分时行情
                Series SeriesStockQuote = new Series("StockQuote2");
                myChart.Series.Add(SeriesStockQuote);
                SeriesStockQuote.BorderColor = Color.White;
                SeriesStockQuote.BorderWidth = 4;
                SeriesStockQuote.BackGradientEndColor = ColorTranslator.FromHtml("100,3,39,110");//050808  #0A0F17
                SeriesStockQuote.Color = ColorTranslator.FromHtml("255,3,39,110");//0000FF  #01286E
                SeriesStockQuote.Type = SeriesChartType.SplineArea;
                SeriesStockQuote.CustomAttributes = "DrawingStyle=LightToDark";
                SeriesStockQuote.BackGradientType = GradientType.TopBottom;

                if (StockQuote2 != null)
                {
                    SeriesStockQuote.Points.DataBindXY(StockQuote2.QuoteDateList, StockQuote2.QuotePriceList);
                }
                SeriesStockQuote.ChartArea = "ChartArea2";

                //处理休市时的时间点
                CustomerPauseLabel(StockQuote2, SeriesStockQuote);

                //处理昨日收盘价
                Series seriesYestodayPrice = new Series("YestodayPrice2");
                myChart.Series.Add(seriesYestodayPrice);

                List<string> QuoteDateListTemplate = CreateQuoteDateListTemplate(StockQuote2);
                if (QuoteDateListTemplate != null) 
                {
                    for (int i = 0; i < QuoteDateListTemplate.Count; i++)
                    {
                        double yestodayPrice = StockQuote2.YestodayPrice;
                        seriesYestodayPrice.Points.AddXY(QuoteDateListTemplate[i], yestodayPrice);
                    }
                }

                seriesYestodayPrice.Type = SeriesChartType.Line;
                seriesYestodayPrice.Color = ColorTranslator.FromHtml("150, 255, 255, 255");
                seriesYestodayPrice.BorderWidth = 3;
                seriesYestodayPrice.BorderStyle = ChartDashStyle.Solid;
                seriesYestodayPrice.ChartArea = "ChartArea2";
            }
        }


        private static void DrawChartAreas(StockQuote StockQuote1, StockQuote StockQuote2, Chart myChart)
        {

            //左侧区域
            if (StockQuote1!=null)
            {
                ChartArea ChartArea1 = new ChartArea();
                ChartArea1.Name = "ChartArea1";
                myChart.ChartAreas.Add(ChartArea1);
                ChartArea1.BackColor = Color.Black;

                //计算Y值区域
                ComputeYArea(StockQuote1, ChartArea1);

                ChartArea1.AxisY.MajorGrid.LineColor = ColorTranslator.FromHtml("120, 255, 255, 255");//
                ChartArea1.AxisY.MajorGrid.LineStyle = ChartDashStyle.Dash;
                ChartArea1.AxisY.MajorGrid.LineWidth = 3;
                ChartArea1.AxisY.LabelStyle.FontColor = Color.White;
                ChartArea1.AxisY.LabelStyle.Format = "F2";
                ChartArea1.AxisY.LabelStyle.Font = new Font("Arial", 18f, FontStyle.Regular);
                ChartArea1.AxisY.LineColor = Color.White;
                ChartArea1.AxisY.MajorTickMark.LineColor = Color.White;
                ChartArea1.AxisY.MinorGrid.Enabled = false;

                //ChartArea1.AxisX.LabelStyle.Font = new Font("Trebuchet MS", 8.25F, FontStyle.Bold);
                ChartArea1.AxisX.Margin = false;
                ChartArea1.AxisX.MajorGrid.Enabled = false;
                ChartArea1.AxisX.MinorGrid.Enabled = false;
                ChartArea1.AxisX.Interval = 60f;
                //ChartArea1.AxisX.IntervalType = DateTimeIntervalType.Minutes;
                ChartArea1.AxisX.LabelStyle.FontColor = Color.White;
                ChartArea1.AxisX.LabelStyle.Font = new Font("Arial", 21f, FontStyle.Regular);
                //ChartArea1.AxisX.LabelStyle.Format = "hh:mm";

                ChartArea1.AxisX.LineColor = Color.White;
                ChartArea1.AxisX.LineWidth = 4;
                ChartArea1.AxisX.MajorTickMark.LineColor = Color.White;

                ChartArea1.Position.X = 0f;
                ChartArea1.Position.Y = 20f;
                ChartArea1.Position.Width = 48.5f;
                ChartArea1.Position.Height = 70f;
            }

            //右侧区域
            if (StockQuote2!=null)
            {
                ChartArea ChartArea2 = new ChartArea();
                ChartArea2.Name = "ChartArea2";
                myChart.ChartAreas.Add(ChartArea2);
                ChartArea2.BackColor = Color.Black;

                //计算Y值区域
                ComputeYArea(StockQuote2, ChartArea2);

                ChartArea2.AxisY.MajorGrid.LineColor = ColorTranslator.FromHtml("120, 255, 255, 255");//
                ChartArea2.AxisY.MajorGrid.LineStyle = ChartDashStyle.Dash;
                ChartArea2.AxisY.MajorGrid.LineWidth = 3;
                ChartArea2.AxisY.LabelStyle.FontColor = Color.White;
                ChartArea2.AxisY.LabelStyle.Format = "F2";
                ChartArea2.AxisY.LabelStyle.Font = new Font("Arial", 18f, FontStyle.Regular);
                ChartArea2.AxisY.LineColor = Color.White;
                ChartArea2.AxisY.MajorTickMark.LineColor = Color.White;
                ChartArea2.AxisY.MinorGrid.Enabled = false;

                //ChartArea2.AxisX.LabelStyle.Font = new Font("Trebuchet MS", 8.25F, FontStyle.Bold);
                ChartArea2.AxisX.Margin = false;
                ChartArea2.AxisX.MajorGrid.Enabled = false;
                ChartArea2.AxisX.MinorGrid.Enabled = false;
                ChartArea2.AxisX.Interval = 60f;
                //ChartArea2.AxisX.IntervalType = DateTimeIntervalType.Minutes;
                ChartArea2.AxisX.LabelStyle.FontColor = Color.White;
                ChartArea2.AxisX.LabelStyle.Font = new Font("Arial", 21f, FontStyle.Regular);
                //ChartArea2.AxisX.LabelStyle.Format = "hh:mm";

                ChartArea2.AxisX.LineColor = Color.White;
                ChartArea2.AxisX.LineWidth = 4;
                ChartArea2.AxisX.MajorTickMark.LineColor = Color.White;
                ChartArea2.Position.X = 50f;
                ChartArea2.Position.Y = 20f;
                ChartArea2.Position.Width = 48f;
                ChartArea2.Position.Height = 70f;
            }
           
        }


        private static void DrawTitles(StockQuote StockQuote1, StockQuote StockQuote2, Chart myChart)
        {
            Title titleTop = new Title();
            titleTop.Name = "Top";
            titleTop.Font = new Font("黑体", 100f, FontStyle.Bold, GraphicsUnit.Pixel);
            titleTop.Position.X = 0f;
            titleTop.Position.Y = 0f;
            titleTop.Position.Width = 100f;
            titleTop.Position.Height = 15f;
            titleTop.Color = Color.White;
            titleTop.Docking = Docking.Top;
            titleTop.Alignment = ContentAlignment.TopLeft;
            titleTop.BackImage = "Picture/ChartBg/top.png";

            float topHeight = 0;//titleTop.Position.Height;

            Title titleShortName1 = new Title();
            titleShortName1.Name = "ShortName1";
            titleShortName1.Font = new Font("黑体", 86f, FontStyle.Bold, GraphicsUnit.Pixel);
            titleShortName1.Position.X = 2f;
            titleShortName1.Position.Y = 2f + topHeight;
            titleShortName1.Position.Width = 20f;
            titleShortName1.Position.Height = 10f;
            titleShortName1.Color = Color.White;
            titleShortName1.Docking = Docking.Top;
            titleShortName1.Alignment = ContentAlignment.TopLeft;

            Title titleShortName2 = new Title();
            titleShortName2.Name = "ShortName2";
            titleShortName2.Font = new Font("黑体", 86f, FontStyle.Bold, GraphicsUnit.Pixel);
            titleShortName2.Position.X = 52f;
            titleShortName2.Position.Y = 2f + topHeight;
            titleShortName2.Position.Width = 20f;
            titleShortName2.Position.Height = 10f;
            titleShortName2.Color = Color.White;
            titleShortName2.Docking = Docking.Top;
            titleShortName2.Alignment = ContentAlignment.TopLeft;


            Title titleCurrentPrice1 = new Title();
            titleCurrentPrice1.Name = "CurrentPrice1";
            titleCurrentPrice1.Font = new Font("Arial", 60f, FontStyle.Bold, GraphicsUnit.Pixel);
            titleCurrentPrice1.Position.X = 2f;
            titleCurrentPrice1.Position.Y = 11f + topHeight;
            titleCurrentPrice1.Position.Width = 18f;
            titleCurrentPrice1.Position.Height = 6f;
            titleCurrentPrice1.Color = Color.White;

            Title titleCurrentPrice2 = new Title();
            titleCurrentPrice2.Name = "CurrentPrice2";
            titleCurrentPrice2.Font = new Font("Arial",60f, FontStyle.Bold, GraphicsUnit.Pixel);
            titleCurrentPrice2.Position.X = 52f;
            titleCurrentPrice2.Position.Y = 11f + topHeight;
            titleCurrentPrice2.Position.Width = 18f;
            titleCurrentPrice2.Position.Height = 6f;
            titleCurrentPrice2.Color = Color.White;

            Title titleSwingPercent1 = new Title();
            titleSwingPercent1.Name = "SwingPercent1";
            titleSwingPercent1.Font = new Font("Arial", 70f, FontStyle.Bold, GraphicsUnit.Pixel);
            titleSwingPercent1.Position.X = 31f;
            titleSwingPercent1.Position.Y = 0f + topHeight;
            titleSwingPercent1.Position.Width = 18f;
            titleSwingPercent1.Position.Height = 18f;
            titleSwingPercent1.Color = Color.White;

            Title titleSwingPercent2 = new Title();
            titleSwingPercent2.Name = "SwingPercent2";
            titleSwingPercent2.Font = new Font("Arial", 70f, FontStyle.Bold, GraphicsUnit.Pixel);
            titleSwingPercent2.Position.X = 81f;
            titleSwingPercent2.Position.Y = 0f + topHeight;
            titleSwingPercent2.Position.Width = 18f;
            titleSwingPercent2.Position.Height = 18f;
            titleSwingPercent2.Color = Color.White;

            //myChart.Titles.Add(titleTop);
            myChart.Titles.Add(titleShortName1);
            myChart.Titles.Add(titleCurrentPrice1);
            myChart.Titles.Add(titleSwingPercent1);

            myChart.Titles.Add(titleShortName2);
            myChart.Titles.Add(titleCurrentPrice2);
            myChart.Titles.Add(titleSwingPercent2);

#if DEBUG
            //titleShortName1.BackColor = Color.Red;
            //titleCurrentPrice1.BackColor = Color.Blue;
            //titleSwingPercent1.BackColor = Color.YellowGreen;
#endif

            if (StockQuote1!=null)
            {
                titleShortName1.Text = StockQuote1.ShortName;
                titleCurrentPrice1.Text = StockQuote1.CurrentPrice;
                titleSwingPercent1.Text = StockQuote1.SwingPercent;

                if (StockQuote1.IsDown)
                {
                    titleSwingPercent1.BackImage = "Picture/ChartBg/compare_die.png";
                    titleCurrentPrice1.Color = ColorTranslator.FromHtml("#00FF00");

                }
                else
                {
                    titleSwingPercent1.BackImage = "Picture/ChartBg/compare_zhang.png";
                    titleCurrentPrice1.Color = ColorTranslator.FromHtml("#FF0000");
                }
            }

            if (StockQuote2!=null)
            {
                titleShortName2.Text = StockQuote2.ShortName;
                titleCurrentPrice2.Text = StockQuote2.CurrentPrice;
                titleSwingPercent2.Text = StockQuote2.SwingPercent;

                if (StockQuote2.IsDown)
                {
                    titleSwingPercent2.BackImage = "Picture/ChartBg/compare_die.png";
                    titleCurrentPrice2.Color = ColorTranslator.FromHtml("#00FF00");

                }
                else
                {
                    titleSwingPercent2.BackImage = "Picture/ChartBg/compare_zhang.png";
                    titleCurrentPrice2.Color = ColorTranslator.FromHtml("#FF0000");
                }
            }
        }

    }
}
